package vikaa.adapter;


import java.util.ArrayList;

import vikaa.bean.photoitem;
import vikaa.timelinepackage.AsyLoadImg;

import vikaa.wowo.R;



import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class PhotoAdapter extends BaseAdapter{
	
	private LayoutInflater mInflater;
	private Context mContext;
	public ArrayList<photoitem> files;
	//SyncImageLoader syncImageLoader;
	
	public PhotoAdapter(Context context,GridView gridView,ArrayList<photoitem> files){
		mInflater = LayoutInflater.from(context);
		mContext = context;
		this.files = files;
	}

	
	public int getCount() {
		return files.size();
	}

	public Object getItem(int position) {
		if(position >= getCount()){
			return null;
		}
		return files.get(position);
	}

	public long getItemId(int position) {
		return position;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		imgHolder wh;
		if(convertView == null){
			convertView = mInflater.inflate(R.layout.photolist_item, null);
			wh = new imgHolder();
        	wh.photolist_item = (ImageView) convertView.findViewById(R.id.photolist_item);
        	wh.selectImg = (ImageView) convertView.findViewById(R.id.select);
        	convertView.setTag(wh);
        }else{
        	wh = (imgHolder)convertView.getTag();
        }
		String file = files.get(position).getPath();
		AsyLoadImg.loadLocalPic(file, wh.photolist_item);
		if(files.get(position).isIsselected()){
			wh.selectImg.setImageResource(R.drawable.photo_selected);
		}else{
			wh.selectImg.setImageResource(R.drawable.photo_unselected);
		}
    	return convertView;
	}
	
	static class imgHolder {
    	public ImageView photolist_item;
    	public ImageView selectImg;
    }
	public void changeState(int position,View view){
		photoitem pi = new photoitem();
		pi.id = files.get(position).id;
		pi.path = files.get(position).path;
		pi.isselected = !(files.get(position).isselected);
		files.set(position, pi);
    	//mModels.get(position).setIsselected(!(mModels.get(position).isselected));
    	notifyDataSetChanged();
    }
	/*SyncImageLoader.OnImageLoadListener imageLoadListener = new SyncImageLoader.OnImageLoadListener(){

		public void onImageLoad(Integer t, Drawable drawable,boolean selected) {
			View view = mGridView.findViewWithTag(t);
			if(view != null){
				ImageView iv = (ImageView) view.findViewById(R.id.photolist_item);
				iv.setBackgroundDrawable(makeBmp(drawable,selected));
			}
		}
		public void onError(Integer t) {
			String model = (String) getItem(t);
			View view = mGridView.findViewWithTag(model);
			if(view != null){
				ImageView iv = (ImageView) view.findViewById(R.id.photolist_item);
				iv.setBackgroundResource(R.drawable.select_image_default);
			}
		}
		
	};*/
	
	private LayerDrawable makeBmp(Drawable drawable, boolean isChosen){
		BitmapDrawable bd = (BitmapDrawable)drawable;
		Bitmap mainBmp = Bitmap.createScaledBitmap(bd.getBitmap(), 100, 100, true);bd.getBitmap(); 
    	Bitmap seletedBmp;
    	LayerDrawable la;
    	if(isChosen == true){
    		seletedBmp = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.photo_selected);
        	Drawable[] array = new Drawable[2];
        	array[0] = new BitmapDrawable(mainBmp);
        	array[1] = new BitmapDrawable(seletedBmp);
        	la = new LayerDrawable(array);
        	la.setLayerInset(0, 0, 0, 0, 0);
        	la.setLayerInset(1, 100, 100, 0, 0 );
    	}
    	else{
    		seletedBmp = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.photo_unselected);
    		Drawable[] array = new Drawable[2];
        	array[0] = new BitmapDrawable(mainBmp);
        	array[1] = new BitmapDrawable(seletedBmp);
        	la = new LayerDrawable(array);
        	la.setLayerInset(0, 0, 0, 0, 0);
        	la.setLayerInset(1, 100, 100, 0, 0 );
    		}
    	return la;	
    }
	
	/*public void loadImage(){
		int start = mGridView.getFirstVisiblePosition();
		int end =mGridView.getLastVisiblePosition();
		if(end >= getCount()){
			end = getCount() -1;
		}
		syncImageLoader.setLoadLimit(start, end);
		syncImageLoader.unlock();
	}
	AbsListView.OnScrollListener onScrollListener = new AbsListView.OnScrollListener() {
		
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			switch (scrollState) {
				case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
					syncImageLoader.lock();
					break;
				case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
					loadImage();
					break;
				case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
					syncImageLoader.lock();
					break;
	
				default:
					break;
			}
			
		}
		
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			// TODO Auto-generated method stub
			
		}
	};*/
}
